/**
 * 
 */
package com.fita.admin.web.controller.client;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.search.client.ClientInterviewRecordSearch;
import com.fita.admin.entity.security.Login;
import com.fita.admin.service.client.ClientInterviewRecordService;
import com.fita.admin.service.client.ClientService;
import com.fita.admin.web.controller.view.Excel2TemplateView;
import com.fita.admin.web.resolver.Secure;
import com.fita.entity.client.Client;
import com.fita.entity.client.ClientInterviewRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 客户访谈记录
 * 
 * @author Guangli Yan
 *
 */
@Controller
@RequestMapping(path = "/client-interview-records")
@MenuSetting("menu-client")
public class ClientInterviewRecordController {

    private final static Logger logger = LoggerFactory.getLogger(ClientInterviewRecordController.class);

    @Autowired
    private ClientInterviewRecordService clientInterviewRecordService;

    @Autowired
    private ClientService clientService;

    /**
     * @param request 
     * @return
     */
    @GetMapping(path = "")
    @MenuSetting("submenu-client-interview-record-management")
    public ModelAndView list(HttpServletRequest request,
                             Page page,
                             ClientInterviewRecordSearch search,
                             @Secure Login login) {

        ModelAndView modelAndView = new ModelAndView("client/client-interview-record/client-interview-record-list");

        if (!login.isAdmin()){
            search.addPrincipalId(login.getId());
        }

        int count = clientInterviewRecordService.count(ClientInterviewRecord.class, search.getPreparedFilter(false));

        if (count > 0){

            List<ClientInterviewRecord> clientInterviewRecords = clientInterviewRecordService.listByPage(ClientInterviewRecord.class,search.getPreparedFilter(false), search.getOrder(), page.with(count));

            modelAndView.addObject("clientInterviewRecords", clientInterviewRecords);

            modelAndView.addObject("page", page);
        }
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));
        
        return modelAndView;
    }


    /**
     *
     * @param clientInterviewRecordId
     * @return
     */
    @GetMapping(path = "/{clientInterviewRecordId}")
    @MenuSetting("submenu-client-interview-record-management")
    public ModelAndView view(@PathVariable Long clientInterviewRecordId) {

        ModelAndView modelAndView = new ModelAndView("client/client-interview-record/client-interview-record-view");

        ClientInterviewRecord clientInterviewRecord = clientInterviewRecordService.load(ClientInterviewRecord.class, clientInterviewRecordId);

        modelAndView.addObject("clientInterviewRecord", clientInterviewRecord);

        return modelAndView;
    }

    /**
     * @return
     */
    @GetMapping(path = "/add")
    @MenuSetting("submenu-client-interview-record-management")
    public ModelAndView add() {

        ModelAndView modelAndView = new ModelAndView("client/client-interview-record/client-interview-record-add");

        List<Client> clients = clientService.loadAll(Client.class);

        modelAndView.addObject("clients", clients);

        return modelAndView;
    }

    /**
     * @return
     */
    @PostMapping(path = "", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @MenuSetting("submenu-client-interview-record-management")
    public HttpResult onAdd(@RequestParam Long clientId,
                            ClientInterviewRecord clientInterviewRecord,
                            @Secure Login login) {
        try {
            clientInterviewRecordService.add(clientId, clientInterviewRecord, login);
            return new HttpResult().success();
        } catch (Exception e) {
            logger.error("#onAdd() error [{}]", e);
            return new HttpResult().fail("500").message("操作失败");
        }
    }

    /**
     * @return
     */
    @GetMapping(path = "/{clientInterviewRecordId}/edit")
    @MenuSetting("submenu-client-interview-record-management")
    public ModelAndView edit(@PathVariable Long clientInterviewRecordId) {

        ModelAndView modelAndView = new ModelAndView("client/client-interview-record/client-interview-record-edit");

        ClientInterviewRecord clientInterviewRecord = clientInterviewRecordService.load(ClientInterviewRecord.class, clientInterviewRecordId);

        modelAndView.addObject("clientInterviewRecord", clientInterviewRecord);

        return modelAndView;
    }

    /**
     *
     * @param clientInterviewRecord
     * @return
     */
    @PatchMapping(path = "/{clientInterviewRecordId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    @MenuSetting("submenu-client-interview-record-management")
    public HttpResult onEdit(@PathVariable Long clientInterviewRecordId, ClientInterviewRecord clientInterviewRecord) {

        HttpResult result = new HttpResult();
        try {
            clientInterviewRecordService.edit(clientInterviewRecordId, clientInterviewRecord);
            return new HttpResult().success();
        } catch (BusinessException e) {
            logger.info("#onEdit() fail. [{}]", e);
            return new HttpResult().fail("500").message(e.getMessage());
        } catch (Exception e){
            return new HttpResult().fail("500");
        }
    }

    /**
     * @return
     */
    @GetMapping(path = "/export")
    @MenuSetting("submenu-client-interview-record-management")
    public ModelAndView export() {

        ModelAndView modelAndView = new ModelAndView("client/client-interview-record/client-interview-record-export");

        List<Client> clients = clientService.loadAll(Client.class);

        modelAndView.addObject("clients", clients);

        return modelAndView;
    }

    /**
     * @return
     */
    @PostMapping(path = "/export")
    @MenuSetting("submenu-client-interview-record-management")
    public ModelAndView onExport(ClientInterviewRecordSearch search, @Secure Login login) {

        ModelAndView modelAndView = new ModelAndView(new Excel2TemplateView("report/client-interview-record-report.xlsx"));

        if (!login.isAdmin()){
            search.addPrincipalId(login.getId());
        }
        List<ClientInterviewRecord> clientInterviewRecords = clientInterviewRecordService.search(search);

        modelAndView.addObject("clientInterviewRecords", clientInterviewRecords);

        return modelAndView;
    }
    
    /**
     * @return
     */
    @GetMapping(path = "/{clientInterviewRecordId}/delete")
    @MenuSetting("submenu-client-interview-record-management")
    public ModelAndView delete(@PathVariable Long clientInterviewRecordId) {

        ModelAndView modelAndView = new ModelAndView("client/client-interview-record/client-interview-record-delete");

        modelAndView.addObject("clientInterviewRecordId", clientInterviewRecordId);

        return modelAndView;
    }

    /**
     * @return
     */
    @RequestMapping(path = "/{clientInterviewRecordId}/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @MenuSetting("submenu-client-interview-record-management")
    @ResponseBody
    public HttpResult onDelete(@PathVariable Long clientInterviewRecordId) {

        try {
            clientInterviewRecordService.delete(clientInterviewRecordId);
            return new HttpResult().success().message("删除成功");
        } catch (BusinessException e) {
            logger.info("#onDelete() fail [{}]", e);
            return new HttpResult().fail("500").message(e.getMessage());
        } catch (Exception e){
            logger.error("#onDelete() error, [{}]", e);
            return new HttpResult().fail("500").message("操作失败");
        }
    }
    

}
